<template>
  <div>
    <!--    搜索学生信息-->
    <div id="search" style="text-align: right">
      <el-form :inline="true" id="demo-form-inline">
        <el-form-item label="学生姓名">
          <el-input @keyup.enter.native="handleSearch" v-model="formDate.name" placeholder="请输入学生姓名"></el-input>
        </el-form-item>
        <el-form-item label="学生入学日期" style="margin-left: 100px;">
          <div class="block">
            <el-date-picker
              v-model="formDate.date"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item style="margin-left: 100px;margin-right: 150px;">
          <el-button style="width: 150px;" type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider></el-divider>
    <!--    展示学生信息-->
    <div id="list">
      <el-table :data="studentList" style="width: 100%" :stripe="true" :border="true" height="420">
        <el-table-column label="学生ID" width="80px" prop="sid"></el-table-column>
        <el-table-column label="学生姓名" width="120px" prop="sname"></el-table-column>
        <el-table-column label="学生年龄" width="120px" prop="age"></el-table-column>
        <el-table-column label="学生性别" width="120px" prop="sex"></el-table-column>
        <el-table-column label="学生邮箱" min-width="200px" prop="email"></el-table-column>
        <el-table-column label="学生学号" min-width="200px" prop="number"></el-table-column>
        <el-table-column label="入学日期" min-width="150px" prop="join_date"></el-table-column>
        <el-table-column label="所属院系" min-width="150px" prop="dep_name"></el-table-column>
        <el-table-column align="center" label="操作" width="200px" fixed="right">
          <template v-slot="scope">
            <el-button size="mini" @click="change(scope.row.sid)">修改</el-button>
            <el-button size="mini" type="danger" @click="open(scope.row.sid)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    修改学生信息-->
    <el-dialog :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="学生ID" :label-width="formLabelWidth">
          <el-input placeholder="请输入内容" v-model="student.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="学生姓名" :label-width="formLabelWidth">
          <el-input v-model="student.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生年龄" :label-width="formLabelWidth">
          <el-input v-model="student.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生性别" :label-width="formLabelWidth">
          <el-radio style="margin-top: 10px" v-model="student.sex" label="男">男</el-radio>
          <el-radio v-model="student.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="学生邮箱" :label-width="formLabelWidth">
          <el-input v-model="student.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生学号" :label-width="formLabelWidth">
          <el-input v-model="student.number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="入学时间" :label-width="formLabelWidth">
          <el-col :span="11">
            <el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                            v-model="student.join_date"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="学生所属院系" :label-width="formLabelWidth">
          <el-select v-model="student.dep_id" clearable placeholder="请选择">
            <el-option
              v-for="item in depList"
              :key="item.id"
              :label="item.depname"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!--    分页-->
    <div id="block">
      <el-pagination background
                     :current-page.sync="pagination.page"
                     :page-size="pagination.pageSize"
                     :total="pagination.total"
                     :page-sizes="[7,15,30]"
                     @current-change="handleCurrentChange"
                     @size-change="handleSizeChange"
                     layout="total, prev, pager, next, sizes">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import departmentApi from "@/api/department";
import studentApi from "@/api/student";
import qs from "qs";
export default {
  name: "list",
  data() {
    return {
      studentList: [],
      depList: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      formDate: {
        name: "",
        date: ""
      },
      pagination: {
        total: 0,
        page: 1,
        pageSize: 7
      },
      timeout: null,
      student: {
        id: "",
        name: "",
        age: '',
        sex: '',
        email: '',
        number: '',
        join_date: "",
        dep_id: '',
        dep_name: ""
      },
    }
  },
  computed: {
    startDate() {
      return this.formDate.date == null ? '' : this.formDate.date[0]
    },
    endDate() {
      return this.formDate.date == null ? '' : this.formDate.date[1]
    }
  },
  mounted() {
    this.queryStudent()
  },
  methods: {
    //页码改变时的回调函数
    handleCurrentChange(page) {
      this.pagination.page = page;
      this.queryStudent();
    },
    //页面大小改变时的回调函数
    handleSizeChange(pageSize) {
      this.pagination.pageSize = pageSize;
      this.queryStudent();
    },
    //打开确认删除的提示框
    open(id) {
      this.$confirm('此操作将永久删除该院系, 是否继续?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(id);
      })
    },
    //修改按钮点击
    change(id) {
      this.selectStudentById(id);
      this.selectAllDepartment();
      this.dialogFormVisible = true;
    },
    //修改学生信息函数
    handleEdit() {
      studentApi.updateStudent(qs.stringify(this.student)).then(resp => {
        if (resp.code === 6666) {
          this.$message.success("修改成功！")
          this.queryStudent()
          this.dialogFormVisible = false;
        } else {
          this.$message.info('修改失败')
        }
      }).catch(() => {
        this.$message.error("修改失败！")
      });
    },
    //删除学生信息函数
    handleDelete(id) {
      studentApi.deleteStudent(id).then(resp => {
        if (resp.code === 6666) {
          this.$message.success('删除成功!');
          this.queryStudent();
        } else {
          this.$message.info('删除失败')
        }
      }).catch(error => {
        this.$message.error('删除出错');
      })
    },
    //点击查询时回到第一页
    handleSearch() {
      this.pagination.page = 1;
      this.queryStudent()
    },
    //分页查询学生信息
    queryStudent() {
      const pagination = {
        name: this.formDate.name,//搜索框中的值
        startDate: this.startDate,//要搜索的时间段的开始日期
        endDate: this.endDate,//要搜索的时间段的结束日期
        page: this.pagination.page,//当前页码
        pageSize: this.pagination.pageSize,//每页多少条数据
      }
      studentApi.getPageStudentList(pagination).then(resp => {
        if (resp.code === 6666) {
          this.studentList = resp.data;
          this.pagination.page = resp.pageInfo.page;
          this.pagination.pageSize = resp.pageInfo.pageSize;
          this.pagination.total = resp.pageInfo.total;
        } else {
          this.$message.info('获取学生列表失败')
        }
      }).catch(() => {
        this.$message.error("查询学生列表出错！")
      })
    },
    //取消修改的提示信息
    handleCancle() {
      this.dialogFormVisible = false;
    },
    //查询学生信息
    selectAllDepartment() {
      departmentApi.getAllList().then(resp => {
        if (resp.code === 6666) {
          this.depList = resp.data;
        } else {
          this.$message.warning('查询院系列表失败')
        }
      }).catch(() => {
        this.$message.error('查询院系列表出错')
      })
    },
    //通过id查询学生信息
    selectStudentById(id){
      studentApi.selectById(id).then(resp=>{
        if (resp.code===6666){
          this.student=resp.data;
        }else {
          this.$message.warning('查询学生信息失败')
        }
      }).catch(()=>{
        this.$message.error('查询学生信息出错')
      })
    }
  },
}
</script>
<style scoped>

#list {
  margin-left: 20px;
  margin-right: 20px;
}

#search {
  margin-left: 100px;
  margin-top: 60px;
}

#block {
  margin-top: 20px;
  margin-left: 20px;
}

#demo-form-inline {
  margin-bottom: 0;
}

.el-divider {
  margin: 10px 0;
}
</style>
